﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PhilipSupport.Controls
{
    public partial class TextBoxFlexible : TextBox
    {
        private Font font = new Font("微软雅黑", 12);
        private SolidBrush msgbrush = new SolidBrush(Color.FromArgb(255, 220, 220, 220));
        private SolidBrush textbrush = new SolidBrush(Color.Black);
        public string PlaceHolder
        {
            get;
            set;
        }

        public TextBoxFlexible()
        {
            InitializeComponent();
            this.BackColor = Color.FromArgb(255, 248, 248, 248);
            this.TextChanged += TextBoxFlexible_TextChanged;
        }

        public void Setting(Color placeHolderColor) {
            msgbrush = new SolidBrush(placeHolderColor);
        }

        private void TextBoxFlexible_TextChanged(object sender, EventArgs e)
        {
            if (this.Text.Length == 0)
            {
                this.SetStyle(ControlStyles.UserPaint, true);
            }
            else
            {
                this.SetStyle(ControlStyles.UserPaint, false);
            }
            this.Refresh();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            if (!string.IsNullOrEmpty(PlaceHolder) && this.Text.Length == 0)
            {
                pe.Graphics.DrawString(PlaceHolder, font, msgbrush, 2, 0);
            }
            base.OnPaint(pe);
        }
    }
}
